﻿using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using System.Collections.Generic;
using UnityEngine;
using Utils;


namespace Event.Command
{
    public class VoteEvent
    {
        List<int> nickNameYes = new List<int>();
        List<int> nickNameNo = new List<int>();

        string voteInfo;
        Coroutine voteCOR;
        

        [PluginEvent(ServerEventType.PlayerGameConsoleCommandExecuted)]
        public void OnCommandExecuted(PlayerGameConsoleCommandExecutedEvent ev)
        {
            if (ev.Command.ToLower().Equals("tvote") || ev.Command.ToLower().Equals("tv"))
            {
                if (ev.Arguments[0].ToLower() == "yes")
                {
                    TryAddYes(ev.Player.PlayerId);
                    return;
                }

                if (ev.Arguments[0].ToLower() == "no")
                {
                    TryAddNo(ev.Player.PlayerId);
                    return;
                }
            }
        }

        [PluginEvent(ServerEventType.RemoteAdminCommandExecuted)]
        public void OnRemoteCommandRun(RemoteAdminCommandExecutedEvent ev)
        {
            if (ev.Command.ToLower().Equals("tvote") || ev.Command.ToLower().Equals("tv"))
            {
                if (ev.Arguments[0] == "done")
                {
                    TimeCoroutine.Stop(voteCOR);
                    voteCOR = null;
                    return;
                }

                if (ev.Arguments[0].ToLower() == "yes") return;
                if (ev.Arguments[0].ToLower() == "no") return;
                if (voteCOR != null) return;

                this.voteInfo = ev.Arguments[0];
                this.DoVoteBroadcast();
            }
        }


        private void DoVoteBroadcast()
        {
            nickNameNo.Clear();
            nickNameYes.Clear();

            voteCOR = TimeCoroutine.Loop(30, 1,
                () =>
                {
                    int yesCount = nickNameYes.Count;
                    int noCount = nickNameNo.Count;

                    MessageStringUtils.GernerateVote(
                        $"<size=28><color=#00FF7F>{voteInfo}</color></size>" +
                        $"\n<color=#00FF00>[同意]</color>{yesCount} " +
                        $"<color=#FF0000>[反对]</color>{noCount} " +
                        $"<color=#FFECA1>[中立]</color>{Player.Count - yesCount - noCount}",
                        out string output);
                    Server.SendBroadcast(output, 1, Broadcast.BroadcastFlags.Normal, true);
                }, () =>
                { 
                    voteCOR = null;
                });
        }


        private void TryAddNo(int playerID)
        {
            if (nickNameYes.Contains(playerID))
            {
                nickNameYes.Remove(playerID);
            }

            if (nickNameNo.Contains(playerID)) return;
            nickNameNo.Add(playerID);
        }

        private void TryAddYes(int playerID)
        {
            if (nickNameNo.Contains(playerID))
            {
                nickNameNo.Remove(playerID);
            }

            if (nickNameYes.Contains(playerID)) return;
            nickNameYes.Add(playerID);
        }
    }
}
